package yf.mall.dysdk.model.request.video;

import com.yf.utils.HttpTookit;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import yf.mall.vo.app.dy.DyVideoVO;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Data
@NoArgsConstructor
@Slf4j
public abstract class DyVideoBaseRequest {

    protected String url;

    protected HttpMethod method;

    protected Map headers;

    protected String token;


//    protected   DyVideoBaseRequest(String url, HttpMethod method) {
//        this.url = url;
//        this.method = method;
//    }


    /**
     * 解析视频
     *
     * @return
     */
    protected String parseVideo(Map params) {
        Long current = System.currentTimeMillis();
        String res;
        if (method.equals(HttpMethod.POST)) {
            res = HttpTookit.doPost(url, params);
        } else {
            res = HttpTookit.doGet(url, params);
        }
        log.info("去水印接口耗时:{}", System.currentTimeMillis()-current );
        return res;
    }

    public abstract DyVideoVO parseVideo(String shareVideo);

    protected String getShareUrl(String text) {
        String regex = "((http|https)://[a-zA-Z0-9./?=_-]+)";
        // 创建Pattern对象
        Pattern pattern = Pattern.compile(regex);
        // 创建Matcher对象
        Matcher matcher = pattern.matcher(text);
        String url = "";
        // 查找匹配的网址
        while (matcher.find()) {
             url = matcher.group();
        }
        return url;
    }



}
